<template>
  <div>
    <el-table :data="tableData.tableArr" style="width: 100%">
      <el-table-column prop="period" label="节次" width="180"></el-table-column>
      <el-table-column prop="start_time" label="起始时间">
        <template slot-scope="scope">
          <el-input v-model="scope.row.start_time" type="time"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="end_time" label="结束时间">
        <template slot-scope="scope">
          <el-input v-model="scope.row.end_time" type="time"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="editRow(scope.$index)">确定修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <p>当前周: {{ currentWeek }}</p>
    <!-- <input type="number" v-model="newWeek" placeholder="输入新的周数">
    <button @click="updateWeek">修改周</button> -->
    <el-input v-model="newWeek" placeholder="输入新的周数"></el-input>
    <el-button @click="updateWeek">修改周</el-button>
  </div>
</template>

<script>
  import axios from 'axios';
export default {
  data() {
    return {
          // 假设的当前周数
          currentWeek: '',
      // 用户输入的新周数
      newWeek: null,
      tableData: {
        tableArr: [
          // { period: '1', startTime: '08:00', endTime: '09:00' },
          // { period: '2', startTime: '09:00', endTime: '10:00' }
        ]
      }
    };
  },
  methods: {
    updateWeek() {
      // 检查用户是否输入了有效的周数
      if (!isNaN(this.newWeek) && this.newWeek > 0 && this.newWeek <= 16) {
        // 更新当前周数
        axios.post('api/admin/setweek',{
          nowweek: this.newWeek
        })
        .then(response => {
          if(response.data.code==1){
            this.getTime();
          }else if(response.data.code==0){
            alert(response.data.msg);
          }else{
            alert('服务器错误，请联系管理员');
          }
        })
        .catch(error => {

        })
        // 可选：清空输入框，以便下次输入
        this.newWeek = null;
      } else {
        // 如果输入无效，给出提示（这里只是简单地在控制台输出）
       alert('请输入有效的周数（1-16）');
      }
    },
    editRow(index) {
      //提出警告是否要修改
      let shouldModify = confirm("你确定要修改吗？");
      if(shouldModify){
        let week=this.tableData.tableArr.filter(item=>item.period==(index+1));
      let timeVo={}
      // console.log(week);
      if(week.length==1){
        timeVo=week[0];
        // console.log(timeVo);
      }
      // 实现修改逻辑
      axios.post('api/admin/settime',timeVo  
      ).then(response => {
        console.log(response.data.data);
      }).catch(error => {});  
    }
      },
    getTime(){
      axios.get('api/admin/gettime')  
      .then(response => {
       this.tableData.tableArr = response.data.data.time;
       this.currentWeek = response.data.data.nowWeek;
      //  console.log(response.data.data[0]);
      }).catch(error => {

      });
    }
  },
  created(){
this.getTime();
  },
  mounted() {
    this.submitForm();
  },
};
</script>

<style>
.el-table {
  width: 100%;
}

</style>